Thread: cannot convert `int (*)[2]' to `int*

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Unhappy cannot convert `int (*)[2]' to `int*

    hello guys! i typed this code used "Code::Blocks",but it said "D:\My Documents\c code\let us c\p288.c|66|error: cannot convert `int (*)[2]' to `int*' in assignment|". why?
    this code is on page296 of "Let us c".
    Code:
    main()
    {
        int s[5][2]={
                        {1234,56},
                        {1212,33},
                        {1434,80},
                        {1312,78}
                     };
        int (*p)[2];
        int i,j,*pint;
    
        for(i=0;i<=3;i++)
        {
            p=&s[i];
            pint=p;
            printf("\n");
            for(j=0;j<=1;j++)
               /* printf("%d ",*(*(p)+j));*/
               printf("%d ",*(pint+j));
        }
    }
    thank you!

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    What else would you expect for "pint=p", since pint is of type "int*" while p is a pointer to an array of [2] ints.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    thank you

    Quote Originally Posted by itCbitC View Post
    What else would you expect for "pint=p", since pint is of type "int*" while p is a pointer to an array of [2] ints.
    This is the program given in Let Us C book.I am following it to learn C. you mean the book is incorrect!

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by luoyangke View Post
    This is the program given in Let Us C book.I am following it to learn C. you mean the book is incorrect!
    Are you sure the code isn't like this instead?
    Code:
    pint = p[0];
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17
    Quote Originally Posted by bithub View Post
    Are you sure the code isn't like this instead?
    Code:
    pint = p[0];
    Code:
    pint=p;
    sure! p296 of Let Us C 5th
    thank you!
    Last edited by luoyangke; 11-11-2009 at 10:48 AM.

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Quote Originally Posted by luoyangke View Post
    Code:
    pint=p;
    sure! p296 of Let Us C 5th
    thank you!
    Sounds like they need a 6th edition then.
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by luoyangke View Post
    This is the program given in Let Us C book.I am following it to learn C. you mean the book is incorrect!
    Yep!

    So either cast the pointer pint = (int *) p; or bithub's way.
    Last edited by itCbitC; 11-11-2009 at 11:18 AM.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by bithub View Post
    Sounds like they need a 6th edition then.
    lol!

  9. #9
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Let Us C is not a good book. I recall a while back seeing a post from someone who was confused about the fact that code from that book wasn't working properly. I scanned through some of the book, and there are numerous problems. Avoid it. If you learn C from Let Us C, be prepared to unlearn some of it when you come into contact with more competent instruction.

  10. #10
    Registered User
    Join Date
    Oct 2009
    Location
    luoyang ,china
    Posts
    17

    Smile thank you

    Quote Originally Posted by cas View Post
    Let Us C is not a good book. I recall a while back seeing a post from someone who was confused about the fact that code from that book wasn't working properly. I scanned through some of the book, and there are numerous problems. Avoid it. If you learn C from Let Us C, be prepared to unlearn some of it when you come into contact with more competent instruction.
    thank you!

    can you recommend some books for me

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. [33.8] Can I convert a pointer-to-function to a void*?
    By EVOEx in forum C++ Programming
    Replies: 40
    Last Post: 08-19-2009, 07:50 AM
  2. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  3. Replies: 3
    Last Post: 08-21-2006, 06:42 AM
  4. Convert Char to Int Function
    By drdroid in forum C++ Programming
    Replies: 9
    Last Post: 02-19-2003, 12:53 PM
  5. please help ... to convert date to string
    By mel in forum C Programming
    Replies: 1
    Last Post: 06-12-2002, 10:26 AM

Tags for this Thread